﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Timer
{
    public partial class Form1 : Form
    {
        //声明当前时间以0.1秒为单位
        private int _nowSecond = 0; 
        public Form1()
        {
            InitializeComponent();
        }
        //点击开始的事件
        private void buttonStart_Click(object sender, EventArgs e)
        {
            //初始化时间
            _nowSecond = Convert.ToInt32(inputBox.Text.Trim()) * 10;
            //启动定时器
            timer1.Enabled = true;
            timer2.Enabled = true;
            //初始化显示值
            labelColon1.Visible = true;
            labelColon2.Visible = true;
            labelHour.Text = "00";
            labelMinute.Text = "00";
            labelSecond.Text = "00.0";
            //启用或禁止相关按钮
            buttonPauseContinue.Enabled = true;
            buttonStop.Enabled = true;
            buttonStart.Enabled = false;
            //改变按钮文字内容
            buttonPauseContinue.Text = "暂停";

        }
        //点击暂停/继续
        private void buttonPauseContinue_Click(object sender, EventArgs e)
        {   
            //可以停止，说明在播放，当前按钮是暂停
            if (buttonStop.Enabled == true)
            {
                //关闭定时器
                timer1.Enabled = false;
                timer2.Enabled = false;
                //显示:
                labelColon1.Visible = true;
                labelColon2.Visible = true;
                //修改按钮文本
                buttonPauseContinue.Text = "继续";
                //启用或禁止相关按钮
                buttonPauseContinue.Enabled = true;
                buttonStop.Enabled = false;
                buttonStart.Enabled = false;
            }
            //当前按钮是继续,设计暂停状态不能点击停止，用于区分是否是暂停
            else 
            {
                //开启定时器
                timer1.Enabled = true;
                timer2.Enabled = true;
                //显示:
                labelColon1.Visible = true;
                labelColon2.Visible = true;
                //修改按钮文本
                buttonPauseContinue.Text = "暂停";
                //启用或禁止相关按钮
                buttonPauseContinue.Enabled = true;
                buttonStop.Enabled = true;
                buttonStart.Enabled = false;
            }
        }
        //点击停止
        private void buttonStop_Click(object sender, EventArgs e)
        {
            //初始化时间
            _nowSecond = Convert.ToInt32(inputBox.Text.Trim()) * 10;
            //关闭定时器
            timer1.Enabled = false;
            timer2.Enabled = false;
            //显示:
            labelColon1.Visible = true;
            labelColon2.Visible = true;
            //启用或禁止相关按钮
            buttonPauseContinue.Enabled = false;
            buttonStop.Enabled = false;
            buttonStart.Enabled = true;
            //改变按钮文字内容
            buttonPauseContinue.Text = "暂停";
        }
        //定义定时器事件，每0.1秒执行一次
        private void timer1_Tick(object sender, EventArgs e)
        {
            //每秒-1(0.1秒为单位)
            _nowSecond--;
            //求出小时，分，秒
            int hour = _nowSecond / 36000;
            int minute = (_nowSecond / 600) % 60;
            double second = Math.Round((_nowSecond % 600 / 10.0), 1);
            //显示事件
            if (hour <= 9)
                labelHour.Text = '0' + hour.ToString();
            else
                labelHour.Text = hour.ToString();
            if (minute <= 9)
                labelMinute.Text = '0' + minute.ToString();
            else
                labelMinute.Text = minute.ToString();
            if (second < 10)
                labelSecond.Text = '0'+ second.ToString();
            else
                labelSecond.Text = second.ToString();
            //倒计时到0结束
            if (_nowSecond == 0) {
                //定时器结束
                timer1.Enabled = false;
                timer2.Enabled = false;
                MessageBox.Show("倒计时结束", "温馨提示", MessageBoxButtons.OKCancel);
                buttonStart.Enabled = true;
                buttonPauseContinue.Enabled = false;
                buttonStop.Enabled = false;
                //设置暂停按钮文字
                buttonPauseContinue.Text = "暂停";
                //:初始可以显示
                labelColon1.Visible = true;
                labelColon2.Visible = true;
            }  
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //每0.5秒切换一次：的显示情况
            if (labelColon1.Visible == true)
            {
                labelColon1.Visible = false;
                labelColon2.Visible = false;
            }
            else
            {
                labelColon1.Visible = true;
                labelColon2.Visible = true;
            }
        }
        //窗口加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            buttonStart.Enabled = true;
            buttonPauseContinue.Enabled = false;
            buttonStop.Enabled = false;
            //设置暂停按钮文字
            buttonPauseContinue.Text = "暂停";
            //:初始可以显示
            labelColon1.Visible = true;
            labelColon2.Visible = true;
        }
        //窗体移动事件
        private void Form1_Move(object sender, EventArgs e)
        {
            //停靠在左右边框
            //屏幕右侧距离屏幕左侧的距离
            int scrennRight = Screen.PrimaryScreen.Bounds.Right;
            //窗体右侧到屏幕左侧距离
            int formRight = this.Left + this.Size.Width;
            if (Math.Abs(scrennRight - formRight) <= 100)
                this.Left = scrennRight - this.Size.Width;
            //距离屏幕左侧距离<=100
            if (Math.Abs(this.Left) <= 100)
                this.Left = 0;

            //停靠在上下边框
            //类似于左右
            int scrennBottom = Screen.PrimaryScreen.Bounds.Bottom;
            int formBottom = this.Top + this.Size.Height;
            if (Math.Abs(scrennBottom - formBottom) <= 60)
                this.Top = scrennBottom - this.Size.Height;
            if (Math.Abs(this.Top) <= 100)
                this.Top = 0;
        }

        private void inputBox_TextChanged(object sender, EventArgs e)
        {
            if (inputBox.Text == "")
                _nowSecond = 0;
            else
                _nowSecond = Convert.ToInt32(inputBox.Text.Trim()) * 10;
        }
    }
}
